/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.guobo.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.modules.guobo.dao.RwDao;
import com.thinkgem.jeesite.modules.guobo.dao.WarningRoleDao;
import com.thinkgem.jeesite.modules.guobo.dao.WarnroleRfidDao;
import com.thinkgem.jeesite.modules.guobo.entity.Rw;
import com.thinkgem.jeesite.modules.guobo.entity.WarningRole;
import com.thinkgem.jeesite.modules.guobo.entity.WarnroleRfid;

/**
 * 预警规则rfid中间表Service
 * @author wudavid
 * @version 2017-03-09
 */
@Service
@Transactional(readOnly = true)
public class WarnroleRfidService extends CrudService<WarnroleRfidDao, WarnroleRfid> {

	@Autowired
	private WarningRoleDao warningRoleDao;
	
	@Autowired
	private RwDao rwDao;
	
	public WarnroleRfid get(String id) {
		return super.get(id);
	}
	
	public List<WarnroleRfid> findList(WarnroleRfid warnroleRfid) {
		return super.findList(warnroleRfid);
	}
	
	public Page<WarnroleRfid> findPage(Page<WarnroleRfid> page, WarnroleRfid warnroleRfid) {
		return super.findPage(page, warnroleRfid);
	}
	
	@Transactional(readOnly = false)
	public void save(WarnroleRfid warnroleRfid) {
		super.save(warnroleRfid);
	}
	
	@Transactional(readOnly = false)
	public void delete(WarnroleRfid warnroleRfid) {
		super.delete(warnroleRfid);
	}
	
	/**
	 * 根据rfid与规则id删除中间表记录
	 * @param rfid
	 * @param roleId
	 */
	@Transactional(readOnly = false)
	public void deleteInfoByRfidRole(WarnroleRfid warnroleRfid){
		dao.deleteInfoByRfidRole(warnroleRfid);
	}
	
	public List<WarnroleRfid> findListByWarnroleId(String warnroleId,Map<String,String> condition){
		WarningRole warningRole = new WarningRole();
		warningRole.setId(warnroleId);
		WarnroleRfid warnroleRfid = new WarnroleRfid();
		if(condition!=null){
			String rfid = condition.get("rfid")+"";
			warnroleRfid.setRfid(rfid);
		}
		warnroleRfid.setWarningRole(warningRole);
		return dao.findDistinctAssetInfoByWarnroleId(warnroleRfid);
	}
	
	/**
	 * 保存预警规则rfid中间表数据
	 * @param warnRoleId 预警规则id
	 * @param rfids rfid编码的数组
	 */
	@Transactional(readOnly = false)
	public void saveWarnroleRfids(String warnRoleId,String[] rfids){
		WarningRole warningRole = warningRoleDao.get(warnRoleId);
		String rwIds = warningRole.getRwIds();
		if(rwIds!=null && !"".equals(rwIds) && !"null".equals(rwIds)){
			String[] ids = rwIds.split(",");
			for (String rwid : ids) {
				Rw rw = rwDao.get(rwid);
				for (String rfid : rfids) {
					WarnroleRfid warnroleRfid=new WarnroleRfid();
					warnroleRfid.setRfid(rfid);
					warnroleRfid.setWarningRole(warningRole);
					warnroleRfid.setRw(rw);
					warnroleRfid.preInsert();
					dao.insert(warnroleRfid);
				}
			}
		}
		
	}
}